package com.huizu.project.service.impl;

import com.github.pagehelper.PageHelper;
import com.huizu.project.domain.*;
import com.huizu.project.mapper.*;
import com.huizu.project.service.IProLdConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.huizu.common.utils.PageUtils.startPage;

/**
 * 基础劳动配置Service业务层处理
 * 
 * @author zhangmx
 * @date 2024-05-11
 */
@Service
public class ProLdConfigServiceImpl implements IProLdConfigService 
{
    @Autowired
    private ProLdConfigMapper proLdConfigMapper;

    @Autowired
    private ProLdJbMapper proLdJbMapper;

    @Autowired
    private ProLdWcMapper proLdWcMapper;

    @Autowired
    private ProLdDjMapper proLdDjMapper;

    @Autowired
    private ProUserLdPointsMapper proUserLdPointsMapper;

    @Autowired
    private ProLdFsMapper proLdFsMapper;

    @Autowired
    private ProUserMapper proUserMapper;

    @Autowired
    private ProLdcgMapper proLdcgMapper;

    @Autowired
    private ProLdgyMapper proLdgyMapper;

    @Autowired
    private ProLdzjMapper proLdzjMapper;

    @Autowired
    private ProLdMapper proLdMapper;


    /**
     * 查询基础劳动配置
     * 
     * @param id 基础劳动配置主键
     * @return 基础劳动配置
     */
    @Override
    public ProLdConfig selectProLdConfigById(String id)
    {
        return proLdConfigMapper.selectProLdConfigById(id);
    }

    /**
     * 查询基础劳动配置列表
     * 
     * @param proLdConfig 基础劳动配置
     * @return 基础劳动配置
     */
    @Override
    public List<ProLdConfig> selectProLdConfigList(ProLdConfig proLdConfig)
    {
        return proLdConfigMapper.selectProLdConfigList(proLdConfig);
    }

    /**
     * 新增基础劳动配置
     * 
     * @param proLdConfig 基础劳动配置
     * @return 结果
     */
    @Override
    public int insertProLdConfig(ProLdConfig proLdConfig)
    {
        return proLdConfigMapper.insertProLdConfig(proLdConfig);
    }

    /**
     * 修改基础劳动配置
     * 
     * @param proLdConfig 基础劳动配置
     * @return 结果
     */
    @Override
    public int updateProLdConfig(ProLdConfig proLdConfig)
    {
        return proLdConfigMapper.updateProLdConfig(proLdConfig);
    }

    /**
     * 批量删除基础劳动配置
     * 
     * @param ids 需要删除的基础劳动配置主键
     * @return 结果
     */
    @Override
    public int deleteProLdConfigByIds(String[] ids)
    {
        return proLdConfigMapper.deleteProLdConfigByIds(ids);
    }

    /**
     * 删除基础劳动配置信息
     * 
     * @param id 基础劳动配置主键
     * @return 结果
     */
    @Override
    public int deleteProLdConfigById(String id)
    {
        return proLdConfigMapper.deleteProLdConfigById(id);
    }


    /**
     * 查询基础劳动配置列表
     *
     * @param proLdConfig 基础劳动配置
     * @return 基础劳动配置集合
     */
    public List<ProLdConfig> selectProLdConfigListApi(ProLdConfig proLdConfig){
        ProUserLdPoints proUserLdPointsPram = new ProUserLdPoints();
        proUserLdPointsPram.setUserId(proLdConfig.getBeUserId());

        List<ProUserLdPoints> proUserLdPoints = proUserLdPointsMapper.selectProUserLdPointsList(proUserLdPointsPram);
        startPage();
        List<ProLdConfig> proLdConfigs = proLdConfigMapper.selectProLdConfigList(new ProLdConfig());
        for (ProLdConfig ldConfig : proLdConfigs) {
            //筛选对应积分
            long count = proUserLdPoints.stream().filter(l -> ldConfig.getType().equals(l.getType())).map(ProUserLdPoints::getPoints).mapToLong(Integer::longValue).sum();
            ldConfig.setUserPosition(count);
        }

        return proLdConfigs;
    }

    /**
     * 查询基础劳动配置列表
     *
     * @param proLdConfig 基础劳动配置
     * @return 基础劳动配置集合
     */
    public Map<String,Object> selectProLdConfigByUserApi(ProLdConfig proLdConfig){
        Map<String,Object> respMap = new HashMap<>();
        ProUserLdPoints proUserLdPointsPram = new ProUserLdPoints();
        proUserLdPointsPram.setUserId(proLdConfig.getBeUserId());
        ProUser proUser = proUserMapper.selectProUserByUserId(proLdConfig.getBeUserId());

        List<ProUserLdPoints> proUserLdPoints = proUserLdPointsMapper.selectProUserLdPointsList(proUserLdPointsPram);
        startPage();
        List<ProLdConfig> proLdConfigs = proLdConfigMapper.selectProLdConfigList(new ProLdConfig());
        for (ProLdConfig ldConfig : proLdConfigs) {
            //筛选对应积分
            long count = proUserLdPoints.stream().filter(l -> ldConfig.getType().equals(l.getType())).map(ProUserLdPoints::getPoints).mapToLong(Integer::longValue).sum();
            ldConfig.setUserPosition(count);
        }
        respMap.put("countPoints",proUser.getMaxPoints());
        respMap.put("userPoints",proUser.getUserPoints());
        respMap.put("list",proLdConfigs);
        return respMap;
    }

    public List<Map<String,Object>> listPhb(ProUser proUser){
        PageHelper.startPage(proUser.getNum(),proUser.getPage());
        List<ProUser> proUserList = proUserMapper.selectProUserList(proUser);
        List<Map<String, Object>> collect = proUserList.stream().map(entity -> {
            Map<String, Object> map = new HashMap<>();
            map.put("userId", entity.getUserId());
            map.put("stNumber", entity.getStNumber());
            map.put("name", entity.getName());
            map.put("bjName", entity.getBjName());
            map.put("xyName", entity.getXyName());
            map.put("njName", entity.getNjName());
            map.put("xxName", entity.getXxName());
            return map;
        }).collect(Collectors.toList());
        return collect;
    }

    public List<ProLdJb> listJb(){
        return proLdJbMapper.selectProLdJbList(new ProLdJb());
    }

    public List<ProLdDj> listDj(){
        return proLdDjMapper.selectProLdDjList(new ProLdDj());
    }

    public List<ProLdWc> listWc(){
        return proLdWcMapper.selectProLdWcList(new ProLdWc());
    }

    /**
     * 获取审批分数列表
     * @return
     */
    public List<ProLdFs> proLdFsList(){
        return proLdFsMapper.selectProLdFsList(new ProLdFs());
    }

    /**
     * 获取劳动统计列表
     */
    public List<Map<String,Object>> listLdUtils(Map<String,Object> map){
        String status = String.valueOf(map.get("status"));
        String userId = String.valueOf(map.get("userId"));
        if (!map.containsKey("status")){
            status = "";
        }
        if (!map.containsKey("userId")){
            userId = "";
        }

        return proLdMapper.getAllLdByUserId(userId,status);
    }

    @Override
    public Map<String,Integer> myLdAnalysis(String userId){
        ProLd proLdPram = new ProLd();
        proLdPram.setUserId(userId);
        List<ProLd> proLds = proLdMapper.selectProLdList(proLdPram);

        ProLdgy proLdgyPram = new ProLdgy();
        proLdgyPram.setUserId(userId);
        List<ProLdgy> proLdgyList = proLdgyMapper.selectProLdgyList(proLdgyPram);

        ProLdcg proLdcgPram = new ProLdcg();
        proLdcgPram.setUserId(userId);
        List<ProLdcg> proLdcgList = proLdcgMapper.selectProLdcgList(proLdcgPram);

        ProLdzj proLdzjPram = new ProLdzj();
        proLdzjPram.setUserId(userId);
        List<ProLdzj> proLdzjList = proLdzjMapper.selectProLdzjList(proLdzjPram);
        //状态 1待审核 2通过 3驳回
        Map<String,Integer> map = new HashMap<>(4);
        List<ProLd> lds = proLds.stream().filter(proLd->"1".equals(proLd.getStatus())).collect(Collectors.toList());
        List<ProLdgy> ldGys  = proLdgyList.stream().filter(proLdgy->"1".equals(proLdgy.getStatus())).collect(Collectors.toList());
        List<ProLdcg> ldCgs = proLdcgList.stream().filter(proLdcg->"1".equals(proLdcg.getStatus())).collect(Collectors.toList());
        List<ProLdzj> ldZjs = proLdzjList.stream().filter(proLdzj->"1".equals(proLdzj.getStatus())).collect(Collectors.toList());
        map.put("adult",lds.size()+ldGys.size()+ldCgs.size()+ldZjs.size());

        lds = proLds.stream().filter(proLd->"2".equals(proLd.getStatus())).collect(Collectors.toList());
        ldGys  = proLdgyList.stream().filter(proLdgy->"2".equals(proLdgy.getStatus())).collect(Collectors.toList());
        ldCgs = proLdcgList.stream().filter(proLdcg->"2".equals(proLdcg.getStatus())).collect(Collectors.toList());
        ldZjs = proLdzjList.stream().filter(proLdzj->"2".equals(proLdzj.getStatus())).collect(Collectors.toList());
        map.put("pass",lds.size()+ldGys.size()+ldCgs.size()+ldZjs.size());

        lds = proLds.stream().filter(proLd->"3".equals(proLd.getStatus())).collect(Collectors.toList());
        ldGys  = proLdgyList.stream().filter(proLdgy->"3".equals(proLdgy.getStatus())).collect(Collectors.toList());
        ldCgs = proLdcgList.stream().filter(proLdcg->"3".equals(proLdcg.getStatus())).collect(Collectors.toList());
        ldZjs = proLdzjList.stream().filter(proLdzj->"3".equals(proLdzj.getStatus())).collect(Collectors.toList());
        map.put("reject",lds.size()+ldGys.size()+ldCgs.size()+ldZjs.size());
        return map;
    }
}
